<?php
class text{
static function simpletext($image, $data){
	$color = imarty_color($image, $data['color'], (int)$data['alpha']);
	imagettftext($image, (double)$data['size'], (double)$data['angle'], (int)$data['x'], (int)$data['y'], $color, (string)$data['font'], trim($data));
}

static function textarea($image,$data){
	$x=(int)$data['x'];
 	$y =(int)$data['y'];
	$w=(int)$data['w'];
	$h=(int)$data['h'];
	$angle=(double)$data['angle'];
	$text=(string)$data;
	$font=(string)$data['font'];
	$color = imarty_color($image, $data['color'], (int)$data['alpha']);
	if($data['size']==null){
		$size = 10;
	}else{
		$size = (double)$data['size'];
	}
	if($data['dial']==null){
		$dial = '-';
	}else{
		$dial = $data['dial'];
	}
	$cool = imagettfbbox ($size, 0, $font, 'A');
	$line_height = $cool[1]-$cool[7];
	$textarea = imagecreatetransparent($w,$h);
	
	$word = explode(' ',$text);
	$count = count($word);
	$n = 0;
	$tmp ='';
	$real='';
	while($n<$count){
		$word[$n] = $word[$n].' ';
		$a = imagettfbbox($size, 0, $font, $tmp.trim($word[$n]));
		$b = imagettfbbox($size, 0, $font, $tmp.$word[$n]);
		if($a[2]-$a[0]<=$w){
			if($b[2] - $b[0] <=$w){
				$tmp .= $word[$n];
			}else{
				$tmp .= trim($word[$n]);
			}
		}else{
			$a = imagettfbbox($size,0,$font, $word[$n]);
			if($a[2] - $a[0]<=$w){
				$real .= $tmp."\n";
				$tmp = $word[$n];
			}else{
				$k = 0;
				$c = strlen($word[$n])-1;
				while($k<$c){
					$a = imagettfbbox($size, 0, $font, $tmp.$word[$n][$k].$dial);
					if($a[2]-$a[0]<=$w){
						$tmp .= $word[$n][$k];
					}else{
						if($k !=0){
							if($k!=$c-1){
								$real .= $tmp.$dial."\n";
							}else{
								$real .= $tmp."\n";
							}
								$tmp = '';
						}
					}
					++$k;
				}
			}
		}
		++$n;
	}
	$real .= rtrim($tmp);
	imagettftext($textarea, $size, 0, 0, $line_height, $color, $font, $real);
	imagecopy($image, $textarea, $x, $y, 0, 0, $w, $h);
}
}
?>